package omm.domain.evolutiongraph;

import java.util.Map;
import org.joda.time.DateTime;

public abstract class AnualPeriodStrategy implements PeriodStrategy {
	
	private final int canvasWidth;
	
	public AnualPeriodStrategy(int canvasWidth) {
		this.canvasWidth = canvasWidth;
	}

	@Override
	public String calculatePeriodsNames(Map<DateTime, Double> periodsValue) {
		String periodsNames = "";
		for (Map.Entry<DateTime, Double> e : periodsValue.entrySet()) {
			DateTime dt = e.getKey();
			Integer month = dt.getMonthOfYear();
			Integer year = dt.getYear();
			periodsNames = periodsNames + month + '/' + year.toString().substring(2) + '|';
			}
		return periodsNames.substring(0, periodsNames.length() - 1);
	}

	@Override
	public Integer calculateBarsWidth() {
		return (int) (canvasWidth / (getPeriodsQuantity() + 1));
	}

	@Override
	public boolean isInPeriod( DateTime modificationDate, DateTime dateTime) {
		return (dateTime.getYear() == modificationDate.getYear() 
				&& dateTime.getMonthOfYear() == modificationDate.getMonthOfYear());
	}
	
	protected abstract Integer getPeriodsQuantity();
	
	@Override
	public Integer calculateBarsSeparation() {
		return (canvasWidth - ( calculateBarsWidth())* getPeriodsQuantity() )/ getPeriodsQuantity();
	}
	
}
